﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;

namespace Billing
{
    public class StringUtils
    {
        public static bool IsAlphaNumeric(string str)
        {
            return Regex.IsMatch(str, "^[a-zA-Z0-9]*$");
        }

        public static bool IsAlphaNumericWithSpace(string str)
        {
            return Regex.IsMatch(str, "^[a-zA-Z0-9 ]*$");
        }

        public static bool IsAlphaNumericWithSpaceAndDot(string str)
        {
            return Regex.IsMatch(str, "^[a-zA-Z0-9 .]*$");
        }

        public static String CleanInput(string str)
        {
            // Replace invalid characters with empty strings.
            return Regex.Replace(str, @"[^\w ]", "");
        }

        public static string TransliterateGeoToEnglish(string src, bool firstUpper)
        {
            if (String.IsNullOrEmpty(src))
            {
                return String.Empty;
            }

            CharEnumerator enumerator = src.GetEnumerator();
            StringBuilder result = new StringBuilder();

            while (enumerator.MoveNext())
            {
                char from = enumerator.Current;
                string to;
                switch (from)
                {
                    case 'ა': { to = "a"; break; }
                    case 'ბ': { to = "b"; break; }
                    case 'გ': { to = "g"; break; }
                    case 'დ': { to = "d"; break; }
                    case 'ე': { to = "e"; break; }
                    case 'ვ': { to = "v"; break; }
                    case 'ზ': { to = "z"; break; }
                    case 'თ': { to = "t"; break; }
                    case 'ი': { to = "i"; break; }
                    case 'კ': { to = "k"; break; }
                    case 'ლ': { to = "l"; break; }
                    case 'მ': { to = "m"; break; }
                    case 'ნ': { to = "n"; break; }
                    case 'ო': { to = "o"; break; }
                    case 'პ': { to = "p"; break; }
                    case 'ჟ': { to = "zh"; break; }
                    case 'რ': { to = "r"; break; }
                    case 'ს': { to = "s"; break; }
                    case 'ტ': { to = "t"; break; }
                    case 'უ': { to = "u"; break; }
                    case 'ფ': { to = "p"; break; }
                    case 'ქ': { to = "k"; break; }
                    case 'ღ': { to = "gh"; break; }
                    case 'ყ': { to = "k"; break; }
                    case 'შ': { to = "sh"; break; }
                    case 'ჩ': { to = "ch"; break; }
                    case 'ც': { to = "ts"; break; }
                    case 'ძ': { to = "dz"; break; }
                    case 'წ': { to = "ts"; break; }
                    case 'ჭ': { to = "ch"; break; }
                    case 'ხ': { to = "kh"; break; }
                    case 'ჯ': { to = "j"; break; }
                    case 'ჰ': { to = "h"; break; }

                    default: to = from.ToString(); break;
                }

                if (firstUpper)
                {
                    to = to.ToUpper();
                    firstUpper = false;
                }

                result.Append(to);
            }

            return result.ToString();

        }

        public static string FormatXml(string xml)
        {
            if (xml == null)
                return "";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            StringWriter strWriter = new StringWriter();
            strWriter.NewLine = "\r\n";

            XmlTextWriter writer = new XmlTextWriter(strWriter);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 1;
            writer.IndentChar = '\t';

            doc.Save(writer);
            return strWriter.ToString();
        }

    }
}
